package j2024.j202407;

import java.util.*;

public class j0721 {
    /**
     * 207
     * @param n
     * @param p
     * @return
     */
    public boolean canFinish(int n, int[][] p) {
        //1.准备工作
        //记录入度
        int[] in = new int[n];
        //2.建图
        Map<Integer, List<Integer>> edges = new HashMap<>();//邻接表存图
        for (int i = 0; i < p.length; i++) {
            int a = p[i][0],b = p[i][1];
            if(!edges.containsKey(b)){
                edges.put(b,new ArrayList<>());
            }
            edges.get(b).add(a);
            in[a]++;
        }
        //3.拓扑排序
        Queue<Integer> q = new LinkedList<>();
        //（1）先把入度为0的点，加入到队列中
        for (int i = 0; i < n; i++) {
            if(in[i]==0){
                q.add(i);
            }
        }
        //(2)bfs
        while(!q.isEmpty()){
            int t = q.poll();
            for(int a:edges.getOrDefault(t,new ArrayList<>())){
                in[a]--;
                if(in[a] == 0){
                    q.add(a);
                }
            }
        }
        //判断是否有环
        for(int x:in){
            if(x!=0){
                return false;
            }
        }
        return true;
    }

    /**
     * 210
     * @param numCourses
     * @param prerequisites
     * @return
     */
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        //入度
        int[] ret = new int[numCourses];
        int r = 0;
        int[] in = new int[numCourses];
        //图
        Map<Integer,List<Integer>> edges = new HashMap<>();
        for (int i = 0; i < prerequisites.length; i++) {
            //b->a
            int a = prerequisites[i][0],b = prerequisites[i][1];
            if(!edges.containsKey(b)){
                edges.put(b,new ArrayList<>());
            }
            edges.get(b).add(a);
            in[a]++;
        }
        Queue<Integer> q = new LinkedList<>();
        for (int i = 0; i < numCourses; i++) {
            if(in[i]==0){
                q.add(i);
            }
        }
        while(!q.isEmpty()){
            int t = q.poll();
            ret[r++] = t;
            for(int a:edges.getOrDefault(t,new ArrayList<>())){
                in[a]--;
                if(in[a]==0){
                    q.add(a);
                }
            }
        }
        for(int a:in){
            if(in[a]!=0){
                return new int[0];
            }
        }
        return ret;
    }
    public int[] findOrder2(int numCourses, int[][] prerequisites) {
        List<List<Integer>> edges = new ArrayList<>();
        for (int i = 0; i < numCourses; i++) {
            edges.add(new ArrayList<>());
        }
        int[] in = new int[numCourses];
        for (int i = 0; i < prerequisites.length; i++) {
            int a = prerequisites[i][0],b = prerequisites[i][1];
            edges.get(b).add(a);
            in[a]++;
        }
        int[] ret = new int[numCourses];
        int r = 0;
        Queue<Integer> q = new LinkedList<>();
        for (int i = 0; i < numCourses; i++) {
            if(in[i]==0){
                q.add(i);
            }
        }
        while(!q.isEmpty()){
            int t = q.poll();
            ret[r++] = t;
            for(int a:edges.get(t)){
                in[a]--;
                if(in[a]==0){
                    q.add(a);
                }
            }
        }
        if(r==numCourses){
            return ret;
        }else {
            return new int[0];
        }
    }

}
